import { ExportData, Menu } from '@/api/types';
import { a } from 'vite-plugin-mock/dist/types-5af01a9a';

type TargetContext = '_self' | '_parent' | '_blank' | '_top';

export const openWindow = (url: string, opts?: { target?: TargetContext; [key: string]: any }) => {
	const { target = '_blank', ...others } = opts || {};
	window.open(
		url,
		target,
		Object.entries(others)
			.reduce((preValue: string[], curValue) => {
				const [key, value] = curValue;
				return [...preValue, `${key}=${value}`];
			}, [])
			.join(',')
	);
};

export const regexUrl = new RegExp(
	'^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
	'i'
);

/**
 * @description 扁平化数组对象(主要用来处理路由菜单)
 * @return array
 * @param argList
 */
export function getFlatArr(argList: any[]) {
	const newList: any[] = JSON.parse(JSON.stringify(argList));
	return newList.reduce((pre: any[], current: any) => {
		let flatArr = [...pre, current];
		if (current.children) flatArr = [...flatArr, ...getFlatArr(current.children)];
		return flatArr;
	}, []);
}

/**
 * @description 递归找出所有面包屑存储到 pinia/vuex 中
 * @param {Array} routeList 所有菜单列表
 * @param {Object} result 输出的结果
 * @param {Array} parent 父级菜单
 * @returns object
 */
export const getAllBreadcrumbList = (routeList: Menu.SysRouteVo[], result: { [key: string]: any } = {}, parent = []) => {
	for (const item of routeList) {
		result[item.path] = [...parent, item];
		if (item.children) getAllBreadcrumbList(item.children, result, result[item.path]);
	}
	return result;
};

//data: 要遍历的数据， target: 查找目标， result: 用于装查找结果的数组
export function findParent(data: Menu.SysRouteVo[], target: string, result: any[]) {
	for (const item of data) {
		if (item.path === target) {
			//将查找到的目标数据加入结果数组中
			//可根据需求unshift(item.id)或unshift(item)
			result.unshift(item.path);
			return true;
		}
		if (item.children && item.children.length > 0) {
			//根据查找到的结果往上找父级节点
			const isFind = findParent(item.children, target, result);
			if (isFind) {
				result.unshift(item.path);
				return true;
			}
		}
	}
	//走到这说明没找到目标
	return false;
}

export const formValidate = (error: Promise<any>) => {
	return error === undefined;
};

export function fileDownload(dataObj: ExportData) {
	try {
		const u8arr = new Uint8Array(dataObj.fileBytes);
		const aLink = document.createElement('a');
		document.body.appendChild(aLink);
		aLink.style.display = 'none';
		aLink.href = window.URL.createObjectURL(new Blob([u8arr]));
		aLink.download = dataObj.fileName;
		aLink.click();
		document.body.removeChild(aLink);

		return true;
	} catch (error) {
		console.error(error);
		return false;
	}
}

const extToMimes = (ext: string) => {
	let type = undefined;
	switch (ext) {
		// 对应图片格式jpg
		case 'jpg':
			type = 'image/jpeg';
			break;
		// 对应图片格式png
		case 'png':
			type = 'image/png';
			break;
		// 对应图片格式jpeg
		case 'jpeg':
			type = 'image/jpeg';
			break;
		// 对应图片格式gif
		case 'gif':
			type = 'image/gif';
			break;
		// 对应图片格式bmp
		case 'bmp':
			type = 'image/bmp';
			break;
		// 对应文本格式txt
		case 'txt':
			type = 'text/plain';
			break;
		// 对应表格格式xls
		case 'xls':
			type = 'application/vnd.ms-excel';
			break;
		// 对应word文档doc格式
		case 'doc':
			type = 'application/msword';
			break;
		// 对应文档格式pdf
		case 'pdf':
			type = 'application/pdf';
			break;
		// 对应表格格式xlsx
		case 'xlsx':
			type = 'application/vnd.ms-excel';
			break;
		// 对应表格格式csv
		case 'csv':
			type = 'text/csv';
			break;
		// 对应的视频格式一般是MPEG-4或者H.264编码的MP4格式
		case 'mp4':
			type = 'video/mp4';
			break;
		// 对应的视频格式一般是AVI格式
		case 'avi':
			type = 'video/x-msvideo';
			break;
		// 对应的视频格式一般是Windows Media Video格式
		case 'WindowsMediaVideo':
			type = 'video/x-ms-wmv';
			break;
		// 对应的视频格式一般是MOV格式，由苹果公司开发的
		case 'mov':
			type = 'video/quicktime';
			break;
		//  对应的视频格式一般是Flash视频格式，由Adobe公司开发的
		case 'flash':
			type = 'video/x-flv';
			break;
		// 对应的视频格式一般是MKV格式，开源免费的多媒体容器格式
		case 'mkv':
			type = 'video/x-matroska';
			break;
		// 对应音频格式mp3
		case 'mp3':
			type = 'audio/mpeg';
			break;
		// 对应音频格式wav
		case 'wav':
			type = 'audio/wav';
			break;
		// 对应音频格式flac
		case 'flac':
			type = 'audio/flac';
			break;
		// 对应音频格式aac
		case 'aac':
			type = 'audio/aac';
			break;
		// 对应音频格式WMA
		case 'wma':
			type = 'audio/x-ms-wma';
			break;
		default:
			type = 'text/plain';
			break;
	}
	return type;
};

export default null;
